<?php
class Helper_Location {
	private static $instance;
	
	public static function newInstance() {
		if (! self::$instance instanceof self) {
			self::$instance = new self ();
		}
		return self::$instance;
	}
	
	public function __construct() {
	
	}
	
	/*public function createOption(array $locations, $prefix=''){
		if(count($locations)<1){
			return '';
		}
		$str='';
		foreach($locations as $cat){
			$str.= '<option value="'. $cat['location_id'].'">'.$prefix . $cat['name'] .'</option>';
			if(isset($cat['child'])){
				$str.=$this->createOption($cat['child'],$prefix.'&nbsp;&nbsp;');
			}
		}
		return $str;
	}*/
	
	public function createOption($prefix,$default_id=0) {
		$top = Model_Mapper_Locations::newInstance ()->getTopLocations ( true, true );
		//print_r($top);
		return $this->_createOption ( $top,$prefix,$default_id );
	}
	
	public function _createOption($top, $prefix = '',$default_id=0) {
		$str = '';
		foreach ( $top as $item ) {
			
			if (Model_Mapper_Locations::newInstance ()->hasChildrens ( $item ['location_id'] )) {
				$str .= '<option disabled="disabled" style="background:#cccccc" value="' . $item ['location_id'] . '">' . $prefix . $item ['name'] . '</option>';
				$str .= $this->_createOption ( Model_Mapper_Locations::newInstance ()->getChildrens ( $item ['location_id'] ), $prefix . $prefix,$default_id );
			}else{
				$selected='';
				if($item['location_id']==$default_id){
					$selected='selected="selected"';
				}else{
					$selected='';
				}
				$str .= '<option value="' . $item ['location_id'] . '" '.$selected.'>' . $prefix . $item ['name'] . '</option>';
			}
		}
		return $str;
	}

}